package com.example.myweather.Adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.myweather.databinding.ActivityMainBinding
import com.example.myweather.databinding.ForecastViewholderBinding
import com.example.myweather.model.ForecastResponseApi
import java.text.SimpleDateFormat
import java.util.Calendar

class ForecastAdapter:RecyclerView.Adapter<ForecastAdapter.ViewHolder>() {
    // 声明绑定变量
    private lateinit var binding: ForecastViewholderBinding

    inner class ViewHolder:RecyclerView.ViewHolder(binding.root)
    // 创建新的 ViewHolder，当 RecyclerView 需要一个新的视图时调用
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ForecastAdapter.ViewHolder {
        val inflater=LayoutInflater.from(parent.context)
        binding= ForecastViewholderBinding.inflate(inflater,parent,false)
        return ViewHolder()
    }
    // 将数据绑定到 ViewHolder 上，当 RecyclerView 需要展示一个位置的数据时调用
    override fun onBindViewHolder(holder: ForecastAdapter.ViewHolder, position: Int) {
        val binding=ForecastViewholderBinding.bind(holder.itemView)
        val date=SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(differ.currentList[position].dtTxt.toString())
        val calendar=Calendar.getInstance()
        calendar.time=date
        // 解析日期时间并设置对应的日期和时间文本
        val dayOfWeekName=when(calendar.get(Calendar.DAY_OF_WEEK)){
            1 ->"Sun"
            2 ->"Mon"
            3 ->"Tue"
            4 ->"Wed"
            5 ->"Thu"
            6 ->"Fri"
            7 ->"Sat"
            else->"-"
        }
        binding.nameDayTxt.text=dayOfWeekName
        val hour=calendar.get(Calendar.HOUR_OF_DAY)
        val amPm=if (hour<12)"am" else "pm"
        val hour12=calendar.get(Calendar.HOUR)
        binding.hourTxt.text=hour12.toString()+amPm
        // 设置温度文本
        binding.tempTxt.text=differ.currentList[position].main?.temp?.let {Math.round(it).toString()+"°" }
        // 根据天气情况设置对应的图标
        val icon=when(differ.currentList[position].weather?.get(0)?.icon.toString()){
            "01d","0n"->"sunny"
            "02d","02n"->"cloudy_sunny"
            "03d","03n"->"cloudy_sunny"
            "04d","04n"->"cloudy"
            "09d","09n"->"rainy"
            "10d","10n"->"rainy"
            "11d","11n"->"storm"
            "13d","13n"->"snowy"
            "50d","50n"->"windy"

            else ->"sunny"
        }
        // 获取对应图标资源的 ID
        val drawableResourceId:Int=binding.root.resources.getIdentifier(
            icon ,
            "drawable",binding.root.context.packageName
        )
        // 使用 Glide 库加载图标资源到 ImageView 中
        Glide.with(binding.root.context)
            .load(drawableResourceId)
            .into(binding.pic)
    }
    // 返回当前列表项的数量
    override fun getItemCount()=differ.currentList.size
    // DiffUtil 回调，用于比较列表项数据的差异
    private val differCallback=object :DiffUtil.ItemCallback<ForecastResponseApi.data>(){
        // 判断两个对象是否表示同一个项目
        override fun areItemsTheSame(
            oldItem: ForecastResponseApi.data,
            newItem: ForecastResponseApi.data
        ): Boolean {
            return oldItem==newItem
        }
        // 判断两个对象的内容是否完全相同
        override fun areContentsTheSame(
            oldItem: ForecastResponseApi.data,
            newItem: ForecastResponseApi.data
        ): Boolean {
            return oldItem==newItem
        }
    }

    // AsyncListDiffer 对象，用于管理列表数据集合和进行差异更新
    val differ=AsyncListDiffer(this,differCallback)
}